<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_hnapay/model')->方法名();
*/
namespace jy_hnapay;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function pay_url($paylog=array()){
		$pay_url='jy_hnapay/pay//paylogid='.$paylog['id'];
		return $pay_url;
	}
	public function request($url,$post=array(),$continue=true,$direct=false,$return_error=false,$isjson=false,$isrequest=false){
		$post=ijson_encode($post);
		$headers = array('Content-Type' => 'application/json');
		$result=cfc('http')->ihttp_json($url,$post);
		if(is_error($result)){			
			$message="访问失败, 错误: {$result['message']}";
			if($return_error){
				return error($message);
			}else{
				exi($message,'error');
			}
		}
		$data=preg_replace('/[\x00-\x1F]/','',$result['content']);
		$data=@json_decode($data,true);
		if($data['errcode']){
			$message=$this->error_code($data['errcode']).' '.$data['errmsg'].' '.$data['errcode'];
			if($return_error || $_SESSION['this']=='custom'){
				//return error($message);
			}else{
				//exi($message,'error');
			}
		}
		if($direct){
			return $result;
		}
		if(!is_array($data) || !$data){
			$message='访问支付接口错误，以下为原始数据：'.$result['content'];
			if($return_error){
				return error($message);
			}else{
				exi($message,'error');
			}
		}
		return $data;
	}
	 /**
     * 生成签名
     * @param $tranCode
     * @param $params
     * @return string
     * @throws Exception
     */
    public static function sign($tranCode, $params){
        $expCode = model('jy_hnapay/constdata')->getExpParamByTranCode($tranCode);
        if (empty($expCode)){
            exi("TranCode coding error！",'error');
        }
        $signParam =  model('jy_hnapay/model')->getStringData($expCode['signField'], $params);
        return model('jy_hnapay/RSAUtils')->sign($signParam);
    }

    public static function verify($tranCode, $params, $signature){
        $expCode = model('jy_hnapay/constdata')->getExpParamByTranCode($tranCode);
        if (empty($expCode)){
            exi("TranCode coding error！",'error');
        }
        $signParam = $this->getStringData($expCode['signField'], $params);
        return $this->model('RSAUtils')->verity($signParam, $signature);
    }

    /**
     * @param $tranCode
     * @param $params
     * @return string
     * @throws Exception
     */
    public static function encrypt($tranCode,$params){
        $expCode = model('jy_hnapay/constdata')->getExpParamByTranCode($tranCode);
        if (empty($expCode)){
            exi("TranCode coding error！",'error');
        }
        $encryptData = model('jy_hnapay/model')->getJsonData($expCode['encryptField'], $params);
        return model('jy_hnapay/RSAUtils')->encrypt($encryptData);
    }

    /**
     * 私钥解密, 本demo中暂时用不到
     * @param $tranCode
     * @param $decrptData
     * @return string
     * @throws Exception
     */
    public static function decrypt($tranCode,$decryptData){
        $expCode = model('jy_hnapay/constdata')->getExpParamByTranCode($tranCode);
        if (empty($expCode)){
            exi("TranCode coding error！",'error');
        }
        return model('jy_hnapay/RSAUtils')->decrypt($decryptData);
    }

    /**
     * @param $data
     * @param $params
     * @return 返回签名明文串
     * @throws Exception
     */
    private static function getStringData($data, $params) {
        $fieldString = "";
        foreach($data as $field){
            if (!isset($params[$field])){
                exi($field."参数无效！",'error');
                break;
            }
            $fieldString .= $field . "=[" . $params[$field] . "]";
        }
        return $fieldString;
    }

    /**
     * @param $data
     * @param $params
     * @return string
     * @throws Exception
     */
    private static function getJsonData($data, $params) {
        $fieldString = "";
        foreach($data as $field){
            if (!isset($params[$field])){
                exi($field."参数无效！",'error');
                break;
            }
            $fieldString[$field] = $params[$field];
        }
        return json_encode($fieldString);
    }

    public static function checkPostParam($data){
        $tranCode = $data['tranCode'];
        if(!$tranCode){
            exi("TranCode coding error！",'error');
        }
        $expCode = model('jy_hnapay/constdata')->getExpParamByTranCode($tranCode);
        if (empty($expCode)){
            exi("TranCode coding error！",'error');
        }
        $flag = 1;
        foreach ($expCode['submitField'] as $index){
            if(!isset($data[$index])){
                $flag = 0;
                break;
            }
        }
        return $flag;
    }
	//错误码
	public function error_code($code, $errmsg = '未知错误') {//获取错误详情
		global $_AR;
		$errors = array(
			'-1' => '系统繁忙',
			'0' => '请求成功',
		);
		$code = strval($code);
		if($errors[$code]) {
			return $errors[$code];
		} else {
			return $errmsg;
		}
	}
}
?>